package com.ego.interceptor;

import com.ego.commons.constant.Constant;
import com.ego.pojo.TbUser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class CheckLoginInterceptor implements HandlerInterceptor {


    @Value("${ego.passport.loginUrl}")
    private String loginUrl;//http://localhost:8084/user/showLogin

    /**
     * 拦截思路
     * 1，如果登录，放行
     * 2，如果没有登录，不放行，并且跳转到登录页面
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        TbUser tbUser = (TbUser) request.getSession().getAttribute(Constant.LOGIN_USER_IN_SESSION);
        System.out.println("tbUser = " + tbUser);
        if(tbUser==null){
            response.sendRedirect(loginUrl);
            return false;
        }

        return true;
    }
}
